﻿using System;
using System.Net;
using SCG.SolarVengeanceEngine.Orders;
using SCG.General;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //Directors
    public class Director : StarShip
    {
        //constructor
        public Director(SVGame game, Player owner, int engines, int directRnge, StarSystem origin)
            : base(game, owner, engines, directRnge, origin)
        {
        }

        //Name
        public override string Name
        {
            get
            {
                return "Director";
            }
        }

        //description
        public override string Description
        {
            get
            {
                return "Director StarShips order any StarShips in Range that can do so to attack the nearest enemy or independent StarSystem.  They can issue their orders on each Impulse that they act, based on their Engine rating.";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.Director;
            }
        }

        //Give orders
        protected internal override void PreMoveProcessing()
        {
            if (!Game.ImpulseActions.CanAct(Engines, Game.Impulse))
                return;

            StarSystem targetSys = null;
            foreach (StarShip target in Owner.StarShips)
                if (target.Order == null && target.CanConquerStarSystems)
                    if (Distance(target) <= Value)
                    {
                        //Find target starsystem
                        if (targetSys == null)
                        {
                            double closestDist = Double.MaxValue;
                            foreach (StarSystem sysTest in Game.StarSystems)
                                if (sysTest.Owner == null || Owner.AlliedWith(sysTest.Owner) == AllianceLevel.None)
                                {
                                    if (sysTest.Owner == Owner)
                                        continue;
                                    double dist = Distance(sysTest);
                                    if (dist < closestDist)
                                    {
                                        closestDist = dist;
                                        targetSys = sysTest;
                                    }
                                }
                        }

                        //If there's a target, set order!
                        if (targetSys != null)
                        {
                            MapObjectOrder moo = new MapObjectOrder(Game, OrderType.TargetStarSystem, targetSys);                         
                            target.Order = moo;                      
                        }
                    }        
        }

        //draw director range
        public override SCGColor RadiusColor
        {
            get
            {
                return DirectorRangeColor;
            }
        }

        //color of range indicator
        private static SCGColor DirectorRangeColor = new SCGColor(64, 0, 255, 0);
    }
}
